package whr.modules.assets.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import whr.modules.assets.entity.InfoEntity;
import whr.modules.assets.service.InfoService;
import whr.common.utils.PageUtils;
import whr.common.utils.R;



/**
 * 资产表
 *
 * @author liuli
 * @email 2547121830.com
 * @date 2024-09-23 11:36:58
 */
@RestController
@RequestMapping("assets/info")
@Slf4j
public class InfoController {
    @Autowired
    private InfoService infoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("assets:info:list")
    public R list(@RequestParam Map<String, Object> params){
        /*log.info("params{}",params);*/
        PageUtils page = infoService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{assetsId}")
    @RequiresPermissions("assets:info:info")
    public R info(@PathVariable("assetsId") Long assetsId){
		InfoEntity info = infoService.getById(assetsId);

        return R.ok().put("info", info);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("assets:info:save")
    public R save(@RequestBody InfoEntity info){
        log.info("info{}",info);
		try {
            infoService.saveByAssetsStatus(info);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("assets:info:update")
    public R update(@RequestBody InfoEntity info){
		infoService.updateById(info);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("assets:info:delete")
    public R delete(@RequestBody Long[] assetsIds){
		infoService.removeByIds(Arrays.asList(assetsIds));

        return R.ok();
    }
    /**
     * 饼图
     */
    @RequestMapping("/pielist")
    public R pieList(){
       List<HashMap<String, Object>> pieList =  infoService.pieList();
        /*System.out.println(pieList.get(0).get("assets_name"));*/
        return R.ok().put("list",pieList);
    }


    /**
     * 获取资产的总数
     */
    @RequestMapping("/getAssetsCount")
    public R getAssetsCount(){
        return R.ok().put("data",infoService.getAssetsCount());
    }

}
